package com.sl.ms.carriage.handler;

import cn.hutool.core.collection.CollUtil;
import com.sl.ms.carriage.domain.dto.WaybillDTO;
import com.sl.ms.carriage.entity.CarriageEntity;
import com.sl.transport.common.exception.SLException;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;
@Component
public class CarriageChainHandler {
    @Resource
    private List<AbstractCarriageChainHandler> chainHandlers;
    private AbstractCarriageChainHandler firstHandler;
    @PostConstruct
    private void constructChain(){
        if(CollUtil.isEmpty(chainHandlers)){
            throw new SLException("找不到责任链!");
        }
        firstHandler=chainHandlers.get(0);
        for(int i=0;i<chainHandlers.size();i++){
            if(i==chainHandlers.size()-1){
                chainHandlers.get(i).setNextHandler(null);
            }else {
                chainHandlers.get(i).setNextHandler(chainHandlers.get(i+1));
            }
        }

    }
    public CarriageEntity findCarriage(WaybillDTO waybillDTO){
        return firstHandler.doHandler(waybillDTO);
    }
}
